package com.java.controller.student;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.java.mapper.LogMapper;
import com.java.mapper.ShuseMapper;
import com.java.mapper.StudentMapper;
import com.java.po.Shuse;
import com.java.po.Student;
import com.java.util.LogUtil;
import com.java.util.MD5Util;

@Controller
@RequestMapping("/student")
@Scope("prototype")
public class StudentController {

	@Autowired
	private StudentMapper studentMapper;

	@Autowired
	private ShuseMapper shuseMapper;

	@Autowired
	private LogMapper logMapper;
	// 构造器
	private Student student = null;
	private LogUtil logUtil = null;
	private Shuse shuse = null;

	public StudentController() {
		logUtil = new LogUtil();
		student = new Student();
		shuse = new Shuse();
	}

	/**
	 * 选取宿舍
	 * 
	 * @param dname
	 * @param homes
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/onShuse", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	@ResponseBody
	private Map<String, Object> onShuse(@RequestParam("dname") String dname, @RequestParam("homes") String homes,
			HttpServletRequest request) throws UnsupportedEncodingException {
		Map<String, Object> map = new HashMap<String, Object>();// 返回map
		Student student = (Student) request.getSession().getAttribute("student");
		shuse.setDname(dname);
		shuse.setHomes(homes);
		shuse.setNums(null);
		Shuse selectOne = shuseMapper.selectOne(shuse);// 查找是否存在这栋宿舍
//		System.out.println(student.getShuse().length() + 1 + "aaaaaaaaaa");
//		int mun = student.getShuse().trim().length();
//		if (mun != 0) {
//			map.put("status", "");
//			map.put("msg", "你已经选择有床位,不用再操作！");
//			return map;
//		}
		if (student.getShuse() != null) {// null值情况
			int indexOf = student.getShuse().indexOf("-");
			if (indexOf != -1) {
				map.put("status", "");
				map.put("msg", "你已经选择有床位,不用再操作！");
				return map;
			}
		}
		if (selectOne != null) {// 存在
			if (selectOne.getNums() == 6) {
				map.put("status", "");
				map.put("msg", "剩余床位以空，请重新选择");
				return map;
			}
			String username = selectOne.getUser() + ":" + student.getUsername();
			shuse.setId(selectOne.getId());
			shuse.setUser(username);
			shuse.setNums(selectOne.getNums() + 1);
			int update = shuseMapper.updateByPrimaryKeySelective(shuse);
			if (update < 0) {
				map.put("status", "");
				map.put("msg", "选取失败");
				return map;
			}
		} else {
			shuse.setUser(student.getUsername());
			shuse.setNums(1);
			int insert = shuseMapper.insert(shuse);
			if (insert <= 0) {
				map.put("status", "");
				map.put("msg", "选取失败");
				return map;
			}
		}
		student.setShuse(dname + "-" + homes);
		studentMapper.updateByPrimaryKeySelective(student);
		map.put("status", 1);
		map.put("msg", "选取成功");
		return map;
	}

	/**
	 * 用户基本信息修改（更新数据）
	 * 
	 * @param student
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.PUT)
	@ResponseBody
	private Map<String, Object> update(@RequestBody Student student, HttpServletRequest request) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			student.setPassword(null);
			if (student.getPhone().length() < 5) {
				map.put("status", 0);
				map.put("msg", "输入手机号有误，请重新输入！");
				return map;
			}
			if (student.getQq().length() < 5) {
				map.put("status", 0);
				map.put("msg", "输入QQ号有误，请重新输入！");
				return map;
			}
			int result = studentMapper.updateByPrimaryKeySelective(student);
			if (result > 0) {
				// request.getSession().setAttribute("student", student);
				// map.put("student", student);
				map.put("status", 1);
				map.put("msg", "数据更新成功");
				logMapper.insertLog(student.getUsername(), logUtil.getREGISTER(), logUtil.getSUCCESS());
			} else {
				map.put("status", 0);
				map.put("msg", "数据更新失败，请联系管理员！");
				return map;
			}
		} catch (Exception e) {
			logMapper.insertLog(student.getUsername(), logUtil.getREGISTER(), logUtil.getERROR());
			map.put("status", 0);
			map.put("msg", "发送未知错误，请联系管理员！");
			e.printStackTrace();
		}
		return map;

	}

	/**
	 * 用户密码修改
	 * 
	 * @param student
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/uppwd", method = RequestMethod.PUT, produces = "application/json; charset=utf-8")
	@ResponseBody
	private Map<String, Object> update(@RequestBody HashMap<String, String> hashMap, HttpServletRequest request)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String password1 = hashMap.get("password1");
			String password2 = hashMap.get("password2");
			if (!password1.equals(password2)) {
				map.put("status", 0);
				map.put("msg", "两次密码不一致，请重新输入！");
				return map;
			}
			Student student = (Student) request.getSession().getAttribute("student");
			student.setPassword(MD5Util.md5Password(password1));
			int result = studentMapper.updateByPrimaryKeySelective(student);
			if (result > 0) {
				map.put("status", 1);
				map.put("msg", "数据更新成功");
				logMapper.insertLog(student.getUsername(), logUtil.getREGISTER(), logUtil.getSUCCESS());
			} else {
				map.put("status", 0);
				map.put("msg", "数据更新失败，请联系管理员！");
				return map;
			}
		} catch (Exception e) {
			logMapper.insertLog(student.getUsername(), logUtil.getREGISTER(), logUtil.getERROR());
			map.put("status", 0);
			map.put("msg", "发送未知错误，请联系管理员！");
			e.printStackTrace();
		}
		return map;

	}

}
